
/**
 * Header View
 */
window.HeaderView = Backbone.View.extend({
	initialize: function() {
		this.render();
	},
	render: function() {
		var model = this.model.toJSON();
		$(this.el).html(this.template(model));
		return this;
	}
});

/**
 * Home view
 */
window.HomeView = Backbone.View.extend({
	initialize: function() {
		this.render();
	},
	render: function() {
		$(this.el).html(this.template());
		return this;
	}
});

/**
 * Dashboard View
 */
 window.DashboardView = Backbone.View.extend({
 	initialize: function() {
 		this.render();
 	},
 	render: function() {
		var self = this;
		var model = self.model.toJSON();
		var page = self.options.page;
		var subPage = self.options.subPage;
 		$(self.el).html(self.template(model));
		
		console.log("page:: " +page+" -- "+subPage);
		//Here load the sub page for the view on the right
		if(page == "item" && subPage == "add") {
			var item = new Item();
			self.itemAddView = new ItemAddView({model:item});

			$('#content', $(self.el)).html('').html(self.itemAddView.el.childNodes);
			
		} else if (page == "item" && subPage == "upload") {
			$('#content', $(self.el)).html('').html(new ItemUploadView().el.childNodes);
		} else {
			//self.profileView = new UserView({model:model});
			console.log(model);
			var user = new User({userId:model.userId});
		
			user.fetch({success: function(){			
				$('#content', $(self.el)).html('').html(new UserView({model:user}).el);
			}});
			//$('#content', $(self.el)).html('').html(self.profileView.el.childNodes);
		}
		
 		return this;
 	}
 });

/**
 * Login View
 */
window.LoginView = Backbone.View.extend({
	initialize: function() {
		this.render();
	},
	render: function() {
		console.log(this.el);
		$(this.el).html(this.template());
		return this;
	},
	events: {
		'change'		: 'change',
		'click .login'	: function(event) {
			var self = this;
			console.log(self);
			self.model.save(null, {
				success: function(model) {
					console.log("Success");
					app.navigate("/",{trigger:true,replace:true});
					utils.showAlert('Success!', 'Logged in successfully', 'alert-success');
				},
				error:function(error) {
					console.log("Errror"+error);
					utils.showAlert('Error', 'Failure Attempt', 'alert-danger');
				}
			});
		}
	},
	change: function(event) {
		utils.hideAlert();
		
		var target = event.target;
		var change = {};
		
		change[target.name] = target.value;
		this.model.set(change);
		
		//Add validation rule here
	}
});
